package chips440.main;

import java.util.HashMap;
import java.applet.Applet;
import java.applet.AudioClip;
import java.net.URL;
import java.awt.image.BufferedImage;
import chips440.internals.Player;
import chips440.tiles.Tile;
import javax.imageio.ImageIO;
import java.io.FileInputStream;

/**
 * This class acts as an in-memory cache for all of the images and sounds used
 * by the game. Since they're all loaded in from files on disk, building large
 * maps becomes an unnecesscecarily complex and time consuming task. This
 * class assures that each image or sound clip is loaded from disk exactly
 * once, and provides a convenient storage mechanism for them by means of
 * various internal mappings and overloaded methods.
 *
 * @author Ian Voysey
 * @version 0.1
 */
public class ResourceCache{
    private static final String S_DIR = "chips440/resources/sound/";
    private static final String IM_DIR = "chips440/resources/images/";

    private static HashMap<String, AudioClip> sound_map = new HashMap<String,AudioClip>();
    private static HashMap<Integer, BufferedImage> player_map = new HashMap<Integer, BufferedImage>();
    private static HashMap<String, BufferedImage> tile_map = new HashMap<String, BufferedImage>();

    /**
     * Returns the image to depict a that particular tile.
     *
     * @param tile the desired tile
     * @throws IllegalArgumentException if no such tile is in the cache or in
     * the on-disk repository
     */
    public static BufferedImage getImage(String tile){
	BufferedImage tmp = tile_map.get(tile);
	if(tmp == null){
	    try{
		tmp = ImageIO.read(new FileInputStream(IM_DIR + tile + ".gif"));
	    } catch (Exception e){
		throw new IllegalArgumentException("no file for tile " + tile);
	    }
	    tile_map.put(tile,tmp);
	}
	return tmp;
    }

    /**
     * Returns the image to depict a that particular player
     *
     * @param player_id the desired player's id
     * @throws IllegalArgumentException if no such player image is in the
     * cache or in the on-disk repository
     */
    public static BufferedImage getImage(int player_id){
	BufferedImage tmp = player_map.get(player_id);
	if(tmp == null){
	    try{
            if(player_id != 0)
                player_id = 1;
            tmp = ImageIO.read(new FileInputStream(IM_DIR + "player" + player_id + ".gif"));
	    } catch (Exception e){
		throw new IllegalArgumentException("no file for player " + player_id);
	    }
	    player_map.put(player_id,tmp);
	}
	return tmp;
    }

    /**
     * Returns the desired sound clip.
     *
     * @param name the name of the desired clip
     * @throws IllegalArgumentException if no such clip is in the cache or in
     * the on-disk repository
     */
    public static AudioClip getSound(String name){
	AudioClip tmp = sound_map.get(name);
	if(tmp == null){
	    try{
                URL url = chips440.client.Client.class.getClassLoader().getResource(S_DIR + name);
		tmp = Applet.newAudioClip(url);
	    } catch (Exception e){
		throw new IllegalArgumentException("no file for sound " + S_DIR+name);
	    }
	    sound_map.put(name, tmp);
	}
	return tmp;
    }
}
